package com.epoint.mobileoa.ui.todolist;

import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.bizlogic.mobileoa.GetFaWenDetailTask;
import com.epoint.mobileoa.datanalysis.XMLHelp;
import com.epoint.mobileoa.model.ArchiveProcess;
import com.epoint.mobileoa.model.Attach;
import com.epoint.mobileoa.model.OperationBtn;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.AlertHelp;
import com.epoint.mobileoa.tools.PropertiesHelp;
import com.epoint.mobileoa.tools.StringHelp;
import com.epoint.mobileoa.tools.ToastHelp;

/**
 * @author liyc
 * @time 2012-2-16 下午10:04:50
 * @annotation 发文xiang
 */
public class ShouWenView extends SuperView {

	public static final int GetShouWenDetail = 1;
	private Button isShowProcessBtn;// 是否显示处理过程
	private WebView wv;
	private WebView process_wv;
	private TextView tv_clbz, tv_tjr, tv_shouwen_line1;
	private LinearLayout ll_shouwen_operation;// 按钮布局
	private LinearLayout ll_shouwen_process;

	private String dqbuzhou;// 当前步骤
	private String tjr;// 提交人

	private List<OperationBtn> btnlist;

	private List<Attach> attachList;// 附件列表

	private String messageitemguid;

	private String operatorGuid;

	private String pviGuid;

	private String workItemGuid;

	private String messageItemGuid;

	String archiveguid;

	private int taskid = 1;

	String typecode;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void initControls() {
		isShowProcessBtn = (Button) findViewById(R.id.process_btn);
		isShowProcessBtn.setOnClickListener(this);
		wv = (WebView) findViewById(R.id.wv_shouwen_content);
		process_wv = (WebView) findViewById(R.id.process_webview);
		process_wv.setVisibility(View.GONE);
		process_wv.setBackgroundColor(0);
		tv_shouwen_line1 = (TextView) findViewById(R.id.tv_shouwen_line1);
		tv_clbz = (TextView) findViewById(R.id.tv_shouwen_chulibuzhou);
		tv_tjr = (TextView) findViewById(R.id.tv_shouwen_tijiaoren);
		top_left_bt.setOnClickListener(this);
		ll_shouwen_operation = (LinearLayout) findViewById(R.id.ll_shouwen_operation);

		ll_shouwen_process = (LinearLayout) findViewById(R.id.ll_shouwen_process);

		wv.setBackgroundColor(0);

		messageitemguid = getIntent().getStringExtra("messageitemguid");
		archiveguid = getIntent().getStringExtra("archiveguid");
		typecode = getIntent().getStringExtra("typecode");
		dqbuzhou = getIntent().getStringExtra("dqbuzhou");
		tjr = getIntent().getStringExtra("tjr");

		top_pb.setVisibility(View.VISIBLE);
		HashMap param = new HashMap();
		param.put("messageitemguid", messageitemguid);
		param.put("archiveguid", archiveguid);
		param.put("typecode", typecode);
		Task task = new GetFaWenDetailTask(this, param);
		MainService.addTask(task);
		hidenProcess();
	}

	/**
	 * @author lilin
	 * @date 2012-8-4 下午3:51:20
	 * @annotation
	 */
	private void hidenProcess() {
		ll_shouwen_process.setVisibility(View.GONE);
		tv_shouwen_line1.setVisibility(View.GONE);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void refresh(Object... param) {
		if (param[0].toString().equals(MainService.NORMAL)) {
			if (taskid == 1) {
				tv_clbz.setText(Html
						.fromHtml("<font color=red>当前步骤:</font><font color=black>"
								+ dqbuzhou + "</font>"));
				tv_tjr.setText(Html
						.fromHtml("<font color=red>提交人:</font><font color=black>"
								+ tjr + "</font>"));

				String bs = param[1].toString();
				String title = StringHelp.getXMLAtt(bs, "title");
				String wenhao = StringHelp.getXMLAtt(bs, "wenhao");
				String remark = StringHelp.getXMLAtt(bs, "remark");
				String miji = StringHelp.getXMLAtt(bs, "miji");
				String filedate = StringHelp.getXMLAtt(bs, "filedate");
				String shouwendate = StringHelp.getXMLAtt(bs, "shouwendate");
				String txtsubject = StringHelp.getXMLAtt(bs, "txtsubject");
				String baocunqx = StringHelp.getXMLAtt(bs, "baocunqx");
				String ShouWenFileNumber = StringHelp.getXMLAtt(bs,
						"ShouWenFileNumber");
				String laiwendep = StringHelp.getXMLAtt(bs, "laiwendep");
				String txtContent = StringHelp.getXMLAtt(bs, "txtContent");
				StringBuilder sbhtml = new StringBuilder();
				sbhtml.append(StringHelp.getSingleFaWenData(this, "标题：", title));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "主题词：",
						txtsubject));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "收文序列：",
						ShouWenFileNumber));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "来文文号：",
						wenhao));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "密级：", miji));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "文件日期：",
						filedate));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "收文日期：",
						shouwendate));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "保存期限：",
						baocunqx));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "来文单位：",
						laiwendep));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "办理结果：",
						txtContent));
				sbhtml.append(StringHelp
						.getSingleFaWenData(this, "备注：", remark));
				wv.loadDataWithBaseURL(null, sbhtml.toString(), "text/html",
						"utf-8", null);

				operatorGuid = StringHelp.getXMLAtt(bs, "operatorGuid");
				pviGuid = StringHelp.getXMLAtt(bs, "pviGuid");
				workItemGuid = StringHelp.getXMLAtt(bs, "workItemGuid");
				messageItemGuid = StringHelp.getXMLAtt(bs, "messageItemGuid");

				String btnStr = StringHelp.getXMLAtt(bs, "operationMessage");
				btnlist = XMLHelp
						.OperationBtnAna("<root>" + btnStr + "</root>");

				if (PropertiesHelp.getProperty(this, "showShouWenBtn").equals(
						"1")) {
					for (int i = 0; i < btnlist.size(); i++) {
						final int position = i;
						final Button btOperation = new Button(this);
						btOperation.setText(btnlist.get(i).operationname);
						btOperation.setTag(i);
						if (getYGColor().equals("yellow")) {
							btOperation.setTextColor(Color.BLACK);
							btOperation
							.setBackgroundResource(R.drawable.btn_white_selectorl);
						} else {
							btOperation.setTextColor(Color.WHITE);
							btOperation
							.setBackgroundResource(R.drawable.btn_blue_selector);
						}
						btOperation.setTextSize(18);

						btOperation.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View v) {
								String ShowOperationPage = btnlist
										.get(position).ShowOperationPage;

								if (ShowOperationPage.equals("1")) {
									String transactors = btnlist.get(position).transactors;
									Intent intent = new Intent(
											getApplicationContext(),
											ProgressTaskDealView.class);
									intent.putExtra("messageItemGuid",
											messageitemguid);
									intent.putExtra("pviGuid", pviGuid);
									intent.putExtra("workItemGuid",
											workItemGuid);
									intent.putExtra("operationGuid",
											btnlist.get(position).operationGuid);
									intent.putExtra("operatorGuid",
											operatorGuid);
									intent.putExtra(
											"transitionGuid",
											btnlist.get(position).transitionGuid);
									intent.putExtra("transactors", transactors);
									intent.putExtra("guid", archiveguid);
									intent.putExtra("step", dqbuzhou);
									intent.putExtra("operateStatus",
											btnlist.get(position).operateStatus);
									intent.putExtra("tag",
											btnlist.get(position).operationname);
									String ShowNextActivit = btnlist
											.get(position).ShowNextActivit;
									intent.putExtra("ShowNextActivit",
											ShowNextActivit);
									intent.putExtra("typecode", typecode);
									startActivity(intent);
								} else {
									showDialog_wait("正在处理...");
									taskid = 2;
									HashMap<String, Object> params = new HashMap<String, Object>();
									params.put("zhusongrenguid", "");
									params.put("chaosongrenguid", "");
									params.put("messageItemGuid",
											messageItemGuid);
									params.put("operatorGuid", operatorGuid);
									params.put("pviGuid", pviGuid);
									params.put("workItemGuid", workItemGuid);
									params.put("operationGuid",
											btnlist.get(position).operationGuid);
									params.put("operateStatus",
											btnlist.get(position).operateStatus);
									params.put("handleOpinion", "");
									params.put(
											"transitionGuid",
											btnlist.get(position).transitionGuid);
									params.put("typecode", typecode);
									Task task = new ShouwenDealTask(
											ShouWenView.this, params);
									MainService.addTask(task);
								}
							}
						});
						ll_shouwen_operation.addView(btOperation);
					}
				}

				String processStr = StringHelp.getXMLAtt(bs, "getlist");
				List<ArchiveProcess> alist = XMLHelp.ArchiveProcessAna("<root>"
						+ processStr + "</root>");
				tv_shouwen_line1.setText(Html
						.fromHtml("<font color=red>处理过程：</font>"));
				process_wv.setBackgroundColor(0);
				process_wv.getSettings().setBuiltInZoomControls(true); // 显示放大缩小
				process_wv.getSettings().setSupportZoom(true); // 可以缩放
				process_wv.getSettings().setDefaultZoom(ZoomDensity.MEDIUM);// 默认缩放模式
				process_wv.loadDataWithBaseURL(null,
						XMLHelp.pingProcessXML(alist), "text/html", "utf-8",
						null);
				// for (int i = 0; i < alist.size(); i++) {
				// TextView tv = new TextView(this);
				// tv.setSingleLine(true);
				// String html = "<font color=blue>步骤:</font><font color=red>"
				// + alist.get(i).step + "</font>"
				// + "<font color=blue> 办理人员:</font><font color=red>"
				// + alist.get(i).dealer + "</font>"
				// + "<font color=blue> 收到时间:</font><font color=red>"
				// + alist.get(i).receiveDate + "</font>"
				// + "<font color=blue> 阅读时间:</font><font color=red>"
				// + alist.get(i).readDate + "</font>"
				// + "<font color=blue> 处理时间:</font><font color=red>"
				// + alist.get(i).dealDate + "</font>"
				// + "<font color=blue> 办理意见:</font><font color=red>"
				// + alist.get(i).opinion + "</font>"
				// + "<font color=blue> 提交人:</font><font color=red>"
				// + alist.get(i).senderName + "</font>";
				// tv.setText(Html.fromHtml(html));
				// tv.setBackgroundColor(Color.YELLOW);
				// ll_shouwen_process.addView(tv);
				// }

				String attachStr = "<root>"
						+ StringHelp.getXMLAtt(bs, "attaches") + "</root>";
				System.out.println(attachStr);
				attachList = XMLHelp.AttachListAna(attachStr);

				if (attachList.size() > 0) {
					top_right_bt.setVisibility(View.VISIBLE);
					top_right_bt.setText("附件");
					top_right_bt.setOnClickListener(this);
				}
			} else if (taskid == 2) {
				HashMap maps = (HashMap) param[1];
				String type = (String) maps.get("type");
				if (type.equals("deal")) {
					String bs = (String) maps.get("bs");
					if (bs.equals("SUCCESS")) {
						ToastHelp.showToast(this, "处理成功！");
						finish();
						ShouWenView.isdeal = true;
						FaWenView.isdeal = true;
					} else {
						ToastHelp.showToast(this, "处理失败！");
					}
					m_Dialog.dismiss();
				}
			}
		}
		top_pb.setVisibility(View.INVISIBLE);
	}

	@Override
	public void onClick(View v) {
		if (v == top_right_bt) {
			AlertHelp
			.showAttachDownloadAlert(this, attachList, messageitemguid);
		} else if (v == top_left_bt) {
			finishDeal();
		} else if (v == isShowProcessBtn) {
			isShowProcess();// 是否显示处理过程
		}
	}

	/**
	 * @author lilin
	 * @date 2012-8-4 下午3:47:05
	 * @annotation 是否显示处理过程
	 */
	private void isShowProcess() {
		if (ll_shouwen_process.getVisibility() == View.VISIBLE) {
			ll_shouwen_process.setVisibility(View.GONE);
		} else {
			ll_shouwen_process.setVisibility(View.VISIBLE);
		}
		if (tv_shouwen_line1.getVisibility() == View.VISIBLE) {
			tv_shouwen_line1.setVisibility(View.GONE);
		} else {
			tv_shouwen_line1.setVisibility(View.VISIBLE);
		}
		if (process_wv.getVisibility() == View.VISIBLE) {
			process_wv.setVisibility(View.GONE);
		} else {
			process_wv.setVisibility(View.VISIBLE);
		}

	}

	@Override
	public void KEYCODE_BACK() {
		finishDeal();
	}

	private void finishDeal() {
		Intent it = new Intent(this, TodolistView.class);
		startActivity(it);
		finish();
	}

	@Override
	public int getLayoutInt() {
		return R.layout.shouwen;
	}

	@Override
	public String setTitle() {
		return "收文详情";
	}

	public static boolean isdeal = false;

	/* (non-Javadoc)
	 * @see com.epoint.mobileoa.bizlogic.SuperView#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		if (isdeal) {
			finishDeal();
		}

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		isdeal = false;
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}

}
